import { GeoCanvasImagePoi } from "@czmSrc/CzmObjects";
import { MultiPointsModifyEditing } from ".";
import { EditingPoint } from "../EditingPoint";
import { getEllipsoidGeodesicCenter } from "@czmSrc/utils";
import { ESJVector3D } from "earthsdk3";

export class ModifyEditingPoint extends EditingPoint {
    constructor(multiPointsModifyEditing: MultiPointsModifyEditing, position?: ESJVector3D) {
        super(multiPointsModifyEditing, position);
        {
            // 真实点添加鼠标操作
            const { menuPoi } = this;
            menuPoi.commands = [
                ['add', '增加控制点', '', MultiPointsModifyEditing.baseImageUrl + 'add.png'],
                ['delete', '删除当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'delete.png'],
                ['modify', '修改当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'modify.png'],
            ];
            menuPoi.clickCommandName = 'modify';
            menuPoi.rightClickCommandName = 'delete';
            this.dispose(menuPoi.commandEvent.disposableOn((commandName: any) => {
                multiPointsModifyEditing.menuPoiCommand(this, commandName);
            }));
            this.dispose(menuPoi.dbclickEvent.disposableOn(() => {
                multiPointsModifyEditing.menuPoiCommand(this, 'dbClick');
            }));
        }
        if (multiPointsModifyEditing.middlePointShow) {
            // 插值编辑添加中间点
            const middlePoi = this.ad(new GeoCanvasImagePoi(multiPointsModifyEditing.czmViewer))
            {
                middlePoi.imageUri = multiPointsModifyEditing.firstControlPointImageUrl ?? MultiPointsModifyEditing.defaults.firstControlPointImageUrl;
                middlePoi.tooltip = `点击增加一个控制点`;
                middlePoi.bgColor = [0, 0, 0, .6];
                middlePoi.fgColor = [1, 1, 1, 1];
                middlePoi.size = [8, 8];
                middlePoi.originRatioAndOffset = [.5, .5, 0, 0];
                {
                    const update = () => {
                        const implEnabled = multiPointsModifyEditing.enabled ?? MultiPointsModifyEditing.defaults.enabled;
                        if (!implEnabled) {
                            middlePoi.enabled = false;
                            middlePoi.show = false;
                            return;
                        }

                        do {
                            const index = multiPointsModifyEditing.editingPoints.indexOf(this);
                            if (index === -1) break;
                            const l = multiPointsModifyEditing.editingPoints.length;
                            if (l === 0) {
                                console.error(`impl.positionEditors.length === 0!`);
                                break;
                            }
                            const loop = multiPointsModifyEditing.loop ?? MultiPointsModifyEditing.defaults.loop;
                            if (!loop && index + 1 >= l) break;
                            const p0 = this.position;
                            if (!p0) break;
                            let nextIndex = (index + 1) % l;
                            const pn = multiPointsModifyEditing.editingPoints.get(nextIndex);
                            const p1 = pn.position;
                            if (!p1) break;
                            const m = getEllipsoidGeodesicCenter(p0, p1);
                            if (!m) break;
                            m[2] -= multiPointsModifyEditing.czmViewer.editingHeightOffset ?? 0;
                            middlePoi.position = m;
                            const canAdding = multiPointsModifyEditing.editingPoints.length < (multiPointsModifyEditing.maxPointsNum ?? MultiPointsModifyEditing.defaults.maxPointsNum);
                            middlePoi.enabled = canAdding;
                            middlePoi.show = canAdding;
                            return;
                        } while (false);
                        middlePoi.enabled = false;
                        middlePoi.show = false;
                    }
                    update();
                    this.dispose(multiPointsModifyEditing.positionsChanged.disposableOn(update));
                    this.dispose(multiPointsModifyEditing.loopChanged.disposableOn(update));
                    this.dispose(multiPointsModifyEditing.enabledChanged.disposableOn(update));
                }

                this.dispose(middlePoi.clickEvent.disposableOn(e => {
                    if (e.button !== 0) return;
                    if (multiPointsModifyEditing.editingPoints.length >= (multiPointsModifyEditing.maxPointsNum ?? MultiPointsModifyEditing.defaults.maxPointsNum)) {
                        return;
                    }
                    const index = multiPointsModifyEditing.editingPoints.indexOf(this);
                    if (index === -1) return;
                    const tempPos: ESJVector3D | undefined = middlePoi.position ? [...middlePoi.position] : undefined
                    if (tempPos) tempPos[2] += multiPointsModifyEditing.czmViewer.editingHeightOffset ?? 0;
                    let currentPosEditor = new ModifyEditingPoint(multiPointsModifyEditing, tempPos);
                    multiPointsModifyEditing.editingPoints.splice(index + 1, 0, currentPosEditor);
                    multiPointsModifyEditing.modify(currentPosEditor);
                }));
            }
        }
    }
}